// pages/blindSeach/blindSeach.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    actiItem:[{}],
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: true,
    page: 1,
    focus:false,
    seachValue:'',
    userInfo:'',
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    this.getSeachData(true);
    wx.showLoading({})
  },
  getSeachData(flag) {//获取数据
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/all', //仅为示例，并非真实的接口地址
      data: { 
        userId: this.data.userInfo.id,
        currentPage:this.data.page,
        param: this.data.seachValue,
        pageNumber: '10',
        openId: 0
       },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data.pp.length < 10) {
            that.setData({
              hasData: false
            })
          } else {
            that.setData({
              hasData: true
            })
          }
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            that.setData({
              actiItem: []
            });
          } else {
            that.setData({
              loadMore: false
            });
          }
          that.data.actiItem = that.data.actiItem.concat(res.data.data.pp);
          that.setData({
            actiItem: that.data.actiItem
          })
        }
      }
    })
  },
  showDetail: function (e) {//查看详情
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/blindDetail/blindDetail?id=' + item.id + '&userId=' + item.userId
    })
  },
  leaveWord: function (e) {//留言
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/blindDetail/blindDetail?isLeave=true&id=' + item.id + '&userId=' + item.userId
    })
  },
  setLove:function(e){//设置取消心动
    var item = e.currentTarget.dataset.item;
    var that=this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/updateXqUserCharmValue', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: { 
        userId: this.data.userInfo.id, 
        loveFlag: item.loveFlag == 'Y' ? 'N' : 'Y',
        otherId: item.userId
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            ['actiItem[' + e.currentTarget.dataset.index + '].loveFlag']: item.loveFlag == 'Y' ? 'N' : 'Y'
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  inputFocus:function(){//搜索框聚焦事件
    this.setData({
      focus:true
    })
  },
  inputBlur: function () {//搜索框失去焦点事件
    this.setData({
      focus: false
    })
  },
  changeInput: function (e) {//搜索框值改变事件
    this.setData({
      seachValue: e.detail.value,
      page:1
    })
    this.getSeachData(true);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page: 1
    })
    this.getSeachData(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasData) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        page: this.data.page + 1
      });
      this.getSeachData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})